package com.xm.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.xm.common.util.PageUtil;
import com.xm.dao.ItemDao;
import com.xm.dao.OrderDao;
import com.xm.dao.UserDao;
import com.xm.pojo.Item;
import com.xm.pojo.Order;
import com.xm.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    OrderDao orderDao;
    @Autowired
    UserDao userDao;
    @Autowired
    ItemDao itemDao;

    @Override
    public void saveOrder(String userName, Integer itemId,Integer num) {
        Item item=itemDao.selectOne(new QueryWrapper<>(new Item().setId(itemId)));
        User user=userDao.selectOne(new QueryWrapper<>(new User().setUsername(userName)));
        Order order=new Order().setItemId(itemId).setUserId(user.getId()).
                setItemName(item.getName()).setUserName(userName).setNum(num);
        orderDao.insert(order);

    }

    @Override
    public void deleteOrder( Integer orderId) {
        orderDao.deleteById(orderId);
    }

    @Override
    public PageInfo<Object> findOrderByUserName(String userName) {

        return PageUtil.startPage().doSelectPageInfo(()->
                orderDao.selectList(new QueryWrapper<>(new Order().setUserName(userName))));
    }
}
